<template>
  <div class="container">
    <leftcont></leftcont>
    <div class="righCont">
      <topcont></topcont>
      <div class="content">
        <div class="payTable">
          <div class="trendTop">虚拟账号({{ total }})</div>
          <div class="tableCont">
            <div class="headFlex">
              <btn
                value="新增"
                style="width: 100px"
                @click="dialogVisible = true"
              ></btn>
              <div class="tableSerchCont">
                <div class="serchItem" style="margin-right: 30px">
                  <el-radio-group @change="getData" v-model="account_type">
                    <el-radio :value="1">程序员</el-radio>
                    <el-radio :label="2">雇主</el-radio>
                  </el-radio-group>
                </div>
                <div class="serchItem" style="width: 250px">
                  <el-input
                    v-model="serchVal"
                    placeholder="请输入账号名称"
                  ></el-input>
                </div>
                <div class="headBtn">
                  <btn value="搜索" @click="serchFn"></btn>
                </div>
              </div>
            </div>
            <el-table :data="tableData" style="width: 100%">
              <el-table-column
                prop="nick_name"
                show-overflow-tooltip
                label="账号昵称"
              >
              </el-table-column>
              <el-table-column show-overflow-tooltip label="账号头像">
                <template #default="scope">
                  <img :src="scope.row.head_img" />
                </template>
              </el-table-column>
              <el-table-column
                v-if="account_type == 1"
                prop="gold_coins"
                label="完善简历"
              >
                <template #default="scope">
                  {{ scope.row.is_cv == 1 ? "未完善" : "已完善" }}
                </template>
              </el-table-column>
              <el-table-column
                v-if="account_type == 2"
                prop="project_demand_count"
                label="需求数量"
              >
              </el-table-column>
              <el-table-column
                v-if="account_type == 2"
                prop="enterprise_position_count"
                label="岗位数量"
              >
              </el-table-column>
              <el-table-column width="180" prop="create_time" label="创建时间">
              </el-table-column>
              <el-table-column
                property="channelCode"
                show-overflow-tooltip
                label="账号状态"
              >
                <template #default="scope">
                  <el-tag type="success" v-if="scope.row.user_status == 1"
                    >正常</el-tag
                  >
                  <el-tag type="danger" v-if="scope.row.user_status == 2"
                    >禁用</el-tag
                  >
                </template>
              </el-table-column>
              <el-table-column label="操作" width="200">
                <template #default="scope">
                  <div class="ctrolBtn">
                    <div
                      v-if="scope.row.user_status == 1"
                      class="text_btn redBtn"
                      @click="checkStatus(scope.row, 3)"
                    >
                      禁用
                    </div>
                    <div
                      v-if="scope.row.user_status == 2"
                      class="text_btn blueBtn"
                      @click="checkStatus(scope.row, 2)"
                    >
                      恢复
                    </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <div class="pageCont">
              <el-pagination
                background
                layout="prev, pager, next"
                :page-size="per_page"
                :current-page="page"
                @current-change="pageFn"
                :total="total"
              >
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog v-model="dialogVisible" title="新增账号" width="500">
      <el-form :model="formInfo" style="max-width: 600px">
        <el-form-item label="账号头像">
          <defUpImg @update:value="gethead_img"></defUpImg>
        </el-form-item>
        <el-form-item label="账号名称">
          <el-input v-model="formInfo.nick_name" />
        </el-form-item>
        <el-form-item label="账号类型">
          <el-radio-group v-model="formInfo.type">
            <el-radio :value="1">程序员账号</el-radio>
            <el-radio :value="2">雇主账号</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="sureAddFn"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import leftcont from "@/components/leftCont.vue";
import topcont from "@/components/topCont.vue";
import defUpImg from "@/components/public/defUpImg.vue";
import btn from "@/components/public/btn.vue";
import {
  virtually_user,
  virtually_userEdit,
  virtually_userAdd,
} from "@/http/api";
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  components: { defUpImg, leftcont, topcont, btn },
  data() {
    return {
      dialogVisible: false,
      serchVal: "",
      tableData: [{}],
      page: 1,
      per_page: 5,
      total: 0,

      account_type: 1,
      formInfo: {
        head_img: "",
        nick_name: "",
        type: 1,
      },
    };
  },
  props: {},
  methods: {
    sureAddFn() {
      let data = this.formInfo;
      virtually_userAdd(data).then((res) => {
        if (res.data.code == 1) {
          ElMessage({
            type: "success",
            message: "新增成功",
          });
          this.formInfo = {
            head_img: "",
            nick_name: "",
            type: 1,
          };
          this.getData();
          this.dialogVisible = false;
        } else {
          ElMessage(res.data.msg);
        }
      });
    }, //新增账号
    gethead_img(src) {
      this.formInfo.head_img = src;
    },
    pageFn(index) {
      this.page = index;
      this.getData();
    },
    serchFn() {
      this.page = 1;
      this.total = 0;
      this.getData();
    },
    checkStatus(item, type) {
      if (type == 2) {
        ElMessageBox.confirm(`确认通过恢复此账号?`, "操作提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          let data = {
            user_id: item.user_id,
            user_status: 1,
            type: this.account_type,
          };
          virtually_userEdit(data).then((res) => {
            if (res.data.code == 1) {
              ElMessage({
                type: "success",
                message: "状态已正常",
              });
              this.getData();
            } else {
              ElMessage(res.data.msg);
            }
          });
        });
      } else {
        ElMessageBox.confirm(`确认禁用此账号`, "操作提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          let data = {
            user_id: item.user_id,
            user_status: 2,
            type: this.account_type,
          };
          virtually_userEdit(data).then((res) => {
            if (res.data.code == 1) {
              ElMessage({
                type: "success",
                message: "账号已禁用",
              });
              this.getData();
            } else {
              ElMessage(res.data.msg);
            }
          });
        });
      }
    },
    getData() {
      var data = {
        type: this.account_type,
        nick_name: this.serchVal,
        page: this.page,
        per_page: this.per_page,
      };
      virtually_user(data)
        .then((res) => {
          if (res.data.code == 1) {
            this.tableData = res.data.data.data; //把最新的数组赋值给tableData
            this.total = res.data.data.total;
          } else {
            ElMessage(res.data.msg);
          }
        })
        .catch((err) => {
          ElMessage("服务器请求超时");
        });
    },
  },
  mounted() {},
  created() {
    this.getData();
  },
};
</script>

<style scoped lang="less">
.container {
  width: 100%;
  display: flex;
  height: 100vh;
  overflow: hidden;
  .righCont {
    display: flex;
    flex-direction: column;
    flex: 1;
    margin-left: 15px;
    height: 100%;
    width: 10px;
    background-color: #f4f8ff;
    .content {
      padding: 20px;
      box-sizing: border-box;
      width: 100%;
      flex: 1;
      .payTable {
        width: 100%;
        height: 100%;
        background-color: #fff;
        padding: 15px;
        border-radius: 5px;
        overflow: auto;
        box-sizing: border-box;
        .trendTop {
          font-weight: bold;
          font-size: 16px;
          cursor: pointer;
        }
        .tableCont {
          width: 100%;
          margin-top: 15px;
          img {
            height: 40px;
          }
          .redBtn {
            color: #f56c6c;
          }
          .blueBtn {
            color: #96a1fe;
          }
          .greenBtn {
            color: #85ce61;
          }
          .blackBtn {
            color: #666;
          }
          .headFlex {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .tableSerchCont {
              width: 100%;
              display: flex;
              justify-content: flex-end;
              margin-bottom: 10px;
              .serchItem {
                width: 150px;
                margin-left: 10px;
              }
              .headBtn {
                margin-left: 10px;
              }
            }
          }
          .ctrolBtn {
            display: flex;
            .text_btn {
              margin: 0 5px;
              cursor: pointer;
              font-size: 14px;
            }
          }
          .pageCont {
            padding: 30px 0;
            display: flex;
            justify-content: center;
          }
        }
      }
    }
  }
}
</style>
